<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="用户姓名" prop="username">
        <el-input v-model="queryParams.username" placeholder="用户姓名" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="用户昵称" clearable @keyup.enter="handleQuery"
                  class="!w-240px" />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="联系电话" clearable @keyup.enter="handleQuery"
                  class="!w-240px" />
      </el-form-item>

      <!-- <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="queryParams.companyName" placeholder="请输入公司名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item> -->

      <el-form-item label="商家类型" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="请选择商家类型" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MERCHANT_TYPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="所在区域" prop="areaId">
        <el-cascader v-model="queryParams.areaId" :options="categoryList" :props="defaultProps" class="!w-240px"
          clearable filterable placeholder="请选择区域" />
      </el-form-item> -->

      <!-- <el-form-item label="账号类型" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择账号类型" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.ACCOUNT_TYPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="商家状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择商家状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MERCHANT_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="商家认证" prop="isAuth">
        <el-select style="width:100%" v-model="queryParams.isAuth" placeholder="请选择商家认证" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MERCHANT_CERTIFICATION)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm(undefined)" v-hasPermi="['wine:business-user:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <!-- <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['wine:business-user:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="用户昵称" align="center" prop="nickname" min-width="150" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="联系电话" align="center" prop="mobile" min-width="160"  />
      <el-table-column label="公司名称" align="center" prop="companyName" min-width="120" />
      <el-table-column label="公司地址" align="center" prop="companyAddress" />
      <el-table-column label="商家类型" align="center" prop="businessType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MERCHANT_TYPE" :value="scope.row.businessType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="所在区域" align="center" prop="areaName" min-width="180" /> -->
      <el-table-column label="证件" align="center" prop="companyImage" min-width="60">
        <template #default="scope">
          <el-image :z-index="99999999" :hide-on-click-modal="true" preview-teleported :src="scope.row.companyImage"
            class="h-30px w-30px" :preview-src-list="[scope.row.companyImage]" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="平台自营" align="center" prop="isSelfSupport" :show-overflow-tooltip="false">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.IS_NO" :value="scope.row.isSelfSupport" />
        </template>
      </el-table-column> -->
      <el-table-column label="商家状态" align="center" prop="status" :show-overflow-tooltip="false">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MERCHANT_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="商家认证" align="center" prop="isAuth" :show-overflow-tooltip="false">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MERCHANT_CERTIFICATION" :value="scope.row.isAuth" />
        </template>
      </el-table-column>
      <el-table-column label="入驻时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="认证时间" align="center" prop="authTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="有效时间" align="center" prop="overTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template #default="scope">
          <!-- <el-button link type="primary" @click="detailForm(scope.row.id)">
            详情
          </el-button> -->

          <el-button link type="primary" @click="openForm(scope.row.id)" v-hasPermi="['wine:business-user:update']">
            编辑
          </el-button>

          <el-button link type="primary" @click="audit(scope.row)" v-hasPermi="['wine:business-user:query']">
            审核
          </el-button>

          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['wine:business-user:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>
  <auditForm ref="auditformRef" @success="getList" />

</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { BusinessUserApi, BusinessUserVO } from '@/api/wine/businessuser'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { defaultProps } from '@/utils/tree'
import auditForm from './auditForm.vue'


/** 商家配置 列表 */
defineOptions({ name: 'BusinessUser' })
const { push } = useRouter() // 路由跳转

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<BusinessUserVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  businessType: undefined,
  areaId: undefined,
  parentId: undefined,
  userId: undefined,
  userType: undefined,
  creditId: undefined,
  companyName: undefined,
  companyImage: undefined,
  creditName: undefined,
  cardImage: undefined,
  liveImage: undefined,
  licenseImage: undefined,
  receiveAddress: undefined,
  deliveryAddress: undefined,
  supplyNum: undefined,
  isSelfSupport: 0,
  buyNum: undefined,
  complainNum: undefined,
  leaveNum: undefined,
  status: undefined,
  isAuth: undefined,
  isOpen: undefined,
  authTime: [],
  overTime: [],
  createTime: [],
  username: undefined,
  nickname: undefined,
  mobile: undefined,
})
const queryFormRef = ref() // 搜索的表单
const auditformRef = ref()
const audit = (row) => {
  auditformRef.value.open(row)
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BusinessUserApi.getBusinessUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (id?: number) => {
  // 修改
  if (typeof id === 'number') {
    push({ name: 'BusinessuserEdit', params: { id } })
    return
  }
  // 新增
  push({ name: 'BusinessuserAdd' })
}

const detailForm = (id?: number) => {
  push({ name: 'BusinessuserDetail', params: { id } })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BusinessUserApi.deleteBusinessUser(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 获取分类的节点的完整结构 */
const categoryList = ref() // 分类树


/** 初始化 **/
onMounted(async () => {
  await getList()
  // 获得分类树
  const data = await BusinessUserApi.getCategoryList({})
  categoryList.value = data
})
</script>
